/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 搜索文件任务
 * @Description *
 * @Package concurrent.blockingQueue
 * @date 2021/3/19 5:06 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package concurrent.blockingQueue.filesearch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;

public class SearchTask implements Runnable {

    private final BlockingQueue<File> queue;
    private final String keyword;

    public SearchTask(BlockingQueue<File> queue, String keyword) {
        this.queue = queue;
        this.keyword = keyword;
    }

    @Override
    public void run() {
        try {
            boolean done = false;
            while (!done) {
                File file = queue.take();
                if (file == FileEnumerationTask.DUMMY) {
                    queue.put(file);
                    done = true;
                } else search(file);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return void
     * @author chentl
     * @description 搜索文件
     * @date 2021/3/19 5:17 下午
     * @params [file]
     */
    public void search(File file) throws FileNotFoundException {
        if (file.getName().contains(keyword))
            System.out.printf("[search_task] 找到文件名: 【%s】 文件路径: 【%s】 \n", file.getName(), file.getPath());
    }
}
